//
//  XJ_ApplicateRecordViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/10.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_ApplicateRecordViewController.h"
#import "XJ_ApplicateRecordCell.h"
#import "XJ_ApplicateRecordModel.h"

@interface XJ_ApplicateRecordViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height_myScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_scrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic ,strong) UITableView *myTableView;
@property (nonatomic ,strong) UIScrollView *myScrollView;
/**当前的页数**/
@property(nonatomic,assign)NSInteger currentPage;
/**总共的页数**/
@property(nonatomic,assign)NSInteger totalPage;
@property(nonatomic,assign) NSInteger pageSize;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation XJ_ApplicateRecordViewController
-(NSMutableArray*)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    /**初始化当前的页数和总共的页数**/
    _currentPage = 1;
    _totalPage =1;
    _pageSize=20;
    
    [self loadMoneyWithDrawList];
}
-(void)creatMainUI{
    
    self.nav_titleLabel.text =@"记录";
    
    //添加tableview
    _myTableView=[[UITableView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, SCREEN_HEIGHT-SafeAreaTopHeight-kTabBarHeight-10) style:(UITableViewStylePlain)];
    _myTableView.delegate=self;
    _myTableView.dataSource=self;
    _myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_contentView addSubview:_myTableView];
    _myTableView.showsHorizontalScrollIndicator =NO;
    _myTableView.showsVerticalScrollIndicator =NO;
    _myTableView.backgroundColor =[UIColor whiteColor];
    //    _myTableView.tableHeaderView =self.tableViewHeaderView;
    self.automaticallyAdjustsScrollViewInsets = NO;
    _myTableView.layer.cornerRadius =4;
    _myTableView.layer.masksToBounds =YES;
    
    
//    _height_myScrollView.constant =1000;    //第二个参数给定下拉刷新触发方法
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshAction)];
    //显示动画(触发selector方法)
    // [header beginRefreshing];
    //显示到tableView
    _scrollView.mj_header = header;
    // 添加上拉刷新
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshAction)];
    _scrollView.mj_footer = footer;
    
    
    _height_myScrollView.constant =SCREEN_HEIGHT-SafeAreaTopHeight-kTabBarHeight-10;
    _up_scrollView.constant =SafeAreaTopHeight;
}
#pragma mark-上啦刷新 下拉刷新

-(void)headerRefreshAction
{
    NSLog(@"下拉刷新");
    _currentPage = 1;
    _totalPage = 1;
    
    [self loadMoneyWithDrawList];
}

-(void)footerRefreshAction
{
    
    NSLog(@"上拉刷新");
    if (_currentPage > _totalPage)
    {
        [MBProgressHUD showError:@"没有更多数据了"];
        [_scrollView.mj_footer endRefreshing];
        [_scrollView.mj_header endRefreshing];
        return;
    }
    [self loadMoneyWithDrawList];
    
}
#pragma mark-UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count>0?_dataArray.count:0;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    XJ_ApplicateRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell =[[[NSBundle mainBundle] loadNibNamed:@"XJ_ApplicateRecordCell" owner:nil options:nil]firstObject];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor =[UIColor clearColor];
    }
    XJ_ApplicateRecordModel *model =_dataArray[indexPath.row];
    cell.model =model;
 
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

#pragma mark--提现申请列表接口
-(void)loadMoneyWithDrawList{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [dic setValue:@(_currentPage) forKey:@"pageNo"];
    [dic setValue:@(_pageSize) forKey:@"pageSize"];
    [XJ_Utils signPost:@"/money/withDrawList?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        if (self.currentPage==1) {
            if (self.dataArray.count>0) {
                [self.dataArray removeAllObjects];
            }
        }
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        NSArray * withDrawList = [dataDic objectForKey:@"withDrawList"];
        
        if (withDrawList.count > 0) {
            
            for (NSDictionary * dic in withDrawList) {
                
                XJ_ApplicateRecordModel * model = [XJ_ApplicateRecordModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
        }
        [self.myTableView reloadData];
        if (self.currentPage==1) {
            [self.myTableView setContentOffset:CGPointMake(0,0) animated:YES];
        }
        //获取总页数
        if (dataDic[@"totalPage"]!=nil&&![dataDic[@"totalPage"] isKindOfClass:[NSNull class]]) {
            
            self.totalPage = [dataDic[@"totalPage"] integerValue];
        }
        /**数据请求成功之后当前页数+1**/
        self.currentPage++;
        [self.scrollView.mj_footer endRefreshing];
        [self.scrollView.mj_header endRefreshing];
        
        /////修改tableview的高度
        CGRect frame =self.myTableView.frame;
        frame.size.height =self.dataArray.count*50+40;
        self.myTableView.frame =frame;
        
        if (self.myTableView.frame.size.height>self.height_myScrollView.constant) {
            self.height_myScrollView.constant =self.myTableView.frame.size.height;
        }
        if (self.currentPage>self.totalPage) {
            
            self.scrollView.mj_footer.state =MJRefreshStateNoMoreData;
            self.scrollView.mj_footer.hidden =YES;
            
            UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
            bottomView.backgroundColor =ClearColor;
            
            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(30*PIX, 0, SCREEN_WIDTH-2*30*PIX, 40)];
            label.backgroundColor =ClearColor;
            label.textColor =RGBCOLOR(170, 167, 167);
            label.textAlignment =NSTextAlignmentCenter;
            label.text =@"没有更多数据了";
            label.font =[UIFont systemFontOfSize:15.0];
            [bottomView addSubview:label];
            self.myTableView.tableFooterView =bottomView;
            
        }else{
            self.scrollView.mj_footer.state =MJRefreshStateIdle;
            self.scrollView.mj_footer.hidden =NO;
            self.myTableView.tableFooterView =nil;
        }
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        [self.scrollView.mj_footer endRefreshing];
        [self.scrollView.mj_header endRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
